package com.zl;

import org.junit.Test;

import java.util.Random;

public class demoTest {
    @Test
    public void test(){
        System.out.println("hello word!");
    }

    /**
     * 千万级数组赋值1秒
     * 千万级使用冒泡排序，比较次数49,999,995,000,000
     * 百万计冒泡排序，   比较次数 499,999,500,000 t= 23491秒 运算力两千一百万。
     * 十万级随机数组 时间 优化选择排序< 选择排序< 冒泡排序
     */
    @Test
    public void testArray(){
        int[] arr = new int[100000];
        Random random = new Random();
        int temp;
        long t1 = System.currentTimeMillis();
        for (int i = 0; i < arr.length; i++) {
            int j = random.nextInt(10000);
            arr[i]=j;
        }
        //冒泡排序
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-1-i; j++) {
                if (arr[j]>arr[j+1]){
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j+1] = temp;
                }
            }
        }
        long t2 = System.currentTimeMillis();
        //System.out.println(Arrays.toString(arr));
        System.out.println("十万级数组随机数赋值时间 t = " + (t2-t1)/1000 + "秒");//1秒
    }

    @Test
    public void testArray2(){
        int[] arr = new int[100000];
        int temp = 0;
        Random random = new Random();
        long t1 = System.currentTimeMillis();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(10000);
        }
        //System.out.println("arr = " + Arrays.toString(arr));
        for (int i = 0; i < arr.length; i++) {
            for (int j = i+1; j < arr.length; j++) {
                if (arr[i]>arr[j]){
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        //System.out.println("arr = " + Arrays.toString(arr));
        long t2 = System.currentTimeMillis();
        System.out.println("十万级数组随机数赋值时间 t = " + (t2-t1)/1000 + "秒");
    }

}
